{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Stack\n",
    "class Empty(Exception):\n",
    "    pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "class ArrayStack(object):\n",
    "    def __init__(self):\n",
    "        self._data=[]\n",
    "        \n",
    "    def __len__(self):\n",
    "        return len(self._data)\n",
    "    \n",
    "    def is_empty(self):\n",
    "        return len(self._data)==0\n",
    "    \n",
    "    def push(self,e):\n",
    "        self._data.append(e)\n",
    "    \n",
    "    def pop(self):\n",
    "        if self.is_empty():\n",
    "            raise Empty('Stack is empty')\n",
    "        return self._data.pop()\n",
    "    \n",
    "    def top(self):\n",
    "        if self.is_empty():\n",
    "            raise Empty('Stack is empty')\n",
    "        return self._data[-1]\n",
    "    \n",
    "    def __repr__(self):\n",
    "        return f'{self._data}'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[]\n",
      "[5]\n",
      "[5, 3]\n",
      "2\n",
      "3\n",
      "[5]\n",
      "[5, 6, 8]\n",
      "8\n"
     ]
    }
   ],
   "source": [
    "s=ArrayStack()\n",
    "print(s)\n",
    "\n",
    "s.push(5)\n",
    "print(s)\n",
    "\n",
    "s.push(3)\n",
    "print(s)\n",
    "print(len(s))\n",
    "\n",
    "print(s.pop())\n",
    "print(s)\n",
    "\n",
    "s.push(6)\n",
    "s.push(8)\n",
    "print(s)\n",
    "print(s.top())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "L=[1,2,3]\n",
    "for e in L:\n",
    "    print(e)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "#逆序\n",
    "def reverse_data(L):\n",
    "    s=ArrayStack()\n",
    "    for e in L:\n",
    "        s.push(e)\n",
    "    L=[]\n",
    "    while not s.is_empty():\n",
    "        L.append(s.pop())\n",
    "    return L"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[16, 9, 4, 1]\n"
     ]
    }
   ],
   "source": [
    "L=[i**2 for i in range(1,5)]\n",
    "L=reverse_data(L)\n",
    "print(L)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "123\n",
      "456\n",
      "789\n",
      "\n",
      "789\n",
      "456\n",
      "123\n",
      "\n"
     ]
    }
   ],
   "source": [
    "#from array_stack import ArrayStack\n",
    "import os\n",
    "def reverse_file(filename):\n",
    "    S=ArrayStack()\n",
    "    \n",
    "    original = open(filename)\n",
    "    for line in original:\n",
    "        S.push(line.rstrip('\\n'))\n",
    "    original.close\n",
    "    \n",
    "    output = open(filename,'w')\n",
    "    while not S.is_empty():\n",
    "        output.write(S.pop()+'\\n')\n",
    "    output.close()\n",
    "    \n",
    "if __name__ =='__main__':\n",
    "    file = open('d:/test.txt','w');\n",
    "    file.write('123\\n')\n",
    "    file.write('456\\n')\n",
    "    file.write('789\\n')\n",
    "    file.close()\n",
    "    \n",
    "    file = open('d:/test.txt');\n",
    "    print(file.read())\n",
    "    file.close()\n",
    "    \n",
    "    reverse_file('d:/test.txt')\n",
    "    \n",
    "    file = open('d:/test.txt');\n",
    "    print(file.read())\n",
    "    file.close()\n",
    "    \n",
    "    os.remove('d:/test.txt')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "In [(5+x)-(y+z)]: delimiters is matched\n"
     ]
    }
   ],
   "source": [
    "#from array_stack import ArrayStack\n",
    "def is_matched(expr) :\n",
    "    left = '({['\n",
    "    right = ')}]'\n",
    "    S = ArrayStack()\n",
    "    for c in expr:\n",
    "        if c in left:\n",
    "            S.push(c)\n",
    "        elif c in right:\n",
    "            if S.is_empty():\n",
    "                return False\n",
    "            if right.index(c)!= left.index(S.pop()):\n",
    "                return False\n",
    "    return S.is_empty()\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    expr ='[(5+x)-(y+z)]'\n",
    "    if is_matched ( expr ) :\n",
    "        print(f\"In { expr }: delimiters is matched\")\n",
    "    else :\n",
    "        print(f\"In { expr }: delimiters is NOT matched\")\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
